Consider the following C function.int strg(char *str){static int temp=...
Explanation:
The given C function 'strg' takes a string as input and recursively counts the number of characters in the string until it reaches the end of the string. The static variable 'temp' is used to store the count of characters.
Execution:
When the function 'strg' is called with the string "abcabcbb", the following steps are executed:
1. The function is called with the string "abcabcbb".
2. The first character of the string is 'a', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
3. The function 'strg' is called again with the remaining string "bcabcbb".
4. The first character of the string is 'b', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
5. The function 'strg' is called again with the remaining string "cabcbb".
6. The first character of the string is 'c', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
7. The function 'strg' is called again with the remaining string "abcbb".
8. The first character of the string is 'a', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
9. The function 'strg' is called again with the remaining string "bcbb".
10. The first character of the string is 'b', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
11. The function 'strg' is called again with the remaining string "cbb".
12. The first character of the string is 'c', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
13. The function 'strg' is called again with the remaining string "bb".
14. The first character of the string is 'b', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
15. The function 'strg' is called again with the remaining string "b".
16. The first character of the string is 'b', which is not equal to NULL. So, the 'temp' variable is incremented by 1.
17. The function 'strg' is called again with the remaining string "".
18. The first character of the string is NULL, so the 'else' part of the function is executed.
19. The value of 'temp', which is 8, is returned.
Therefore, the output of the function 'strg("abcabcbb")' is 8.
Consider the following C function.int strg(char *str){static int temp=...
This question is related to recursion. hence loop itself.
because function strg calling itself with parameters. outer function strg (char *str) read single character starting from first character and function strg (++str) is incredible. there is 8 character. hence loop executive 8 time and function return value 8. hence answer is 8.
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).